# delimit ;
clear ;
set more off ;
est drop _all ;
cd "R:/personlig/fenellac/menarche_replication/analysis/" ;

capture log close ;
log using "./temp/log-for-app-F-figure01-sensitivity-analysis.log", text replace ;

global plusdir "R:/personlig/fenellac/stata_ado/plus/" ;
sysdir set PLUS $plusdir ;
adopath ++ $plusdir ;

* install moremata ; 
net from "R:/personlig/fenellac/stata_ado/plus/temp/moremata" ;
capture net uninstall moremata ;
net install moremata ;

* **************************************************************************** ;
* This code replicates Appendix F, Figure 1.
*
* Paper: "Age of Marriage and Women's Political Engagement: Evidence from India"
* Authors: Fenella Carpena, Francesca Jensenius
* 
* Code by Fenella Carpena
* Last update: May 2, 2020
* **************************************************************************** ;

****************************************************************************** ;
* select the sample
****************************************************************************** ;

use "./input/eligible-women-ihds02-lhs.dta", clear ;
merge 1:1 IDHH PERSONID using "./input/eligible-women-ihds02-rhs.dta" ;
tab _merge ;
assert _merge == 3 ;
drop _merge ;

* keep only rural women ;
keep if rural == 1 ;

* keep only women who are married only once (i.e., have not ever remarried) ;
drop if ever_remarried == 1 ;
  
* keep only women age of menarche between 11-18 (1st-99th pctile) ; 
summ menarche_age ;
keep if menarche_age >= 11 & menarche_age <= 18 ;

* generating district identifier ;
egen dt_id = group(STATEID DISTID) ;

****************************************************************************** ;
* set-up data for analysis 
****************************************************************************** ;

* rename this variable to make it shorter ;
ren num_children_with nchild ;
ren years_schooling yrssch ;
ren discuss_politics discuss ;
ren member_pol_org member ;

* label variables ; 
label var attended "Attended village council meeting last year" ;
label var discuss "Discusses politics and community with husband" ;
label var member "Participates in a political organization" ;
label var yrssch "Years of schooling" ;
label var nchild "No. children living w/ respondent" ;
label var menarche_age "Menarche age" ;

* make sure the sample is the same across all regressions 
* i.e., remove any observation which has missing values for the relevant variables, 
* because otherwise, each regression has a different sample and the regressions
* will not be comparable ; 
foreach var of varlist 
attended discuss member
yrssch nchild
menarche_age 
height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ {;
	drop if `var' == . ;
} ; 

* remove district FEs manually to ease estimation later ; 
foreach var of varlist 
attended discuss member
yrssch nchild
menarche_age 
height_measure age hindu muslim sc st obc mother_yrs_educ father_yrs_educ { ;
	qui reg `var' i.dt_id  ;
	predict r`var', resid ; 
} ;

* control vars ;
local rcvars "rheight_measure rage rhindu rmuslim rsc rst robc rmother_yrs_educ rfather_yrs_educ" ;

****************************************************************************** ;
****************************************************************************** ;
* 		MEDIATOR: YEARS OF EDUCATION
****************************************************************************** ;
****************************************************************************** ;

****************************************************************************** ;
* Y: attended meeting, M: years_schooling
****************************************************************************** ;
preserve ; 

local mediator "yrssch" ;
local yvar "attended" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;

twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;				
					
* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ; 

****************************************************************************** ;
* Y: discusses politics at home,  M: years schooling
****************************************************************************** ;
preserve ;

local mediator "yrssch" ;
local yvar "discuss" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;

twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;
			
* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ;

****************************************************************************** ;
* Y: member pol org,  M: years schooling
****************************************************************************** ;
preserve ;

local mediator "yrssch" ;
local yvar "member" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;

twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;
					
* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ;

****************************************************************************** ;
****************************************************************************** ;
* 		MEDIATOR: NUMBER OF CHILDREN LIVING WITH RESPONDENT 
****************************************************************************** ;
****************************************************************************** ;

****************************************************************************** ;
* Y: attended meeting, M: # of children living w/ respondent 
****************************************************************************** ;
preserve ;

local mediator "nchild" ;
local yvar "attended" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;

twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;		
		
* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ;

****************************************************************************** ;
* Y: discusses politics at home,  M: # of children living w/ respondent 
****************************************************************************** ;

preserve ;

local mediator "nchild" ;
local yvar "discuss" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;

twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;					

* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ;

****************************************************************************** ;
* Y: member pol org,  M: # of children living w/ respondent 
****************************************************************************** ;

preserve; 

local mediator "nchild" ;
local yvar "member" ; 
local treat "menarche_age" ;

medsens	(regress r`mediator' r`treat' `rcvars' ) 
		(regress r`yvar' r`treat' r`mediator' `rcvars'), 
		treat(r`treat') mediate(r`mediator') sims(500) ;
					
twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

* save graph ;
graph export "./output/figure-sensitivity-Y-`yvar'-M-`mediator'.png", replace ;	
			
* save data ;
saveold "./temp/data-for-figure-sensitivity-Y-`yvar'-M-`mediator'.dta", replace version(12) ;

restore ;

log close ;
exit ;

